﻿using OneRed.Data;
using System.Collections.ObjectModel;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace OneRed.Main.Controls
{
    /// <summary>
    /// Pagination.xaml 的交互逻辑
    /// </summary>
    public partial class PaginationDataTable : UserControl
    {
        public PaginationDataTable()
        {
            InitializeComponent();
            this.Loaded += delegate
            {
                //首页
                this.btnFirst.MouseLeftButtonUp += new MouseButtonEventHandler(btnFirst_Click);
                this.btnFirst.MouseLeftButtonDown += new MouseButtonEventHandler(btnFirst_MouseLeftButtonDown);
                //上一页
                this.btnPrev.MouseLeftButtonUp += new MouseButtonEventHandler(btnPrev_Click);
                this.btnPrev.MouseLeftButtonDown += new MouseButtonEventHandler(btnPrev_MouseLeftButtonDown);
                //下一页
                this.btnNext.MouseLeftButtonUp += new MouseButtonEventHandler(btnNext_Click);
                this.btnNext.MouseLeftButtonDown += new MouseButtonEventHandler(btnNext_MouseLeftButtonDown);
                //末页
                this.btnLast.MouseLeftButtonUp += new MouseButtonEventHandler(btnLast_Click);
                this.btnLast.MouseLeftButtonDown += new MouseButtonEventHandler(btnLast_MouseLeftButtonDown);
                //this.btnGo.Click += new RoutedEventHandler(btnGo_Click);

                //变换每页显示的数量
                this.cbPageSize.SelectionChanged += new SelectionChangedEventHandler(cbPageNum_Choose);
            };
        }
        private DataTable _dt = new DataTable();
        //每页显示多少条
        private int pageNum = 10;
        //当前是第几页
        private int pIndex = 1;
        //对象
        private DataGrid grdList;
        //最大页数
        private int MaxIndex = 1;
        //一共多少条
        private int allNum = 0;

        #region 初始化数据

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="grd"></param>
        /// <param name="dtt"></param>
        /// <param name="Num"></param>
        public void ShowPages(DataGrid grd, DataTable ds, int Num)
        {
            if (ds == null || ds.Rows.Count == 0)
                return;
            if (ds.Rows.Count == 0)
                return;
            DataTable dt = ds;
            this._dt = dt.Clone();
            this.grdList = grd;
            this.pageNum = Num;
            this.pIndex = 1;
            foreach (DataRow r in dt.Rows)
                this._dt.ImportRow(r);
            SetMaxIndex();
            ReadDataTable();
            //if (this.MaxIndex > 1)
            //{
            //    this.pageGo.IsReadOnly = false;
            //    this.btnGo.IsEnabled = true;
            //}
        }

        public void ShowPages(DataGrid grd, ObservableCollection<FormatData> ds, int Num)
        {
            if (ds == null || ds.Count == 0)
                return;

            this.grdList = grd;
            this.pageNum = Num;
            this.pIndex = 1;           
            SetMaxIndex();
            ReadDataTable();
        }

        #endregion

        #region 画数据

        /// <summary>
        /// 画数据
        /// </summary>
        private void ReadDataTable()
        {
            try
            {
                DataTable tmpTable = new DataTable();
                tmpTable = this._dt.Clone();
                int first = this.pageNum * (this.pIndex - 1);
                first = (first > 0) ? first : 0;
                //如果总数量大于每页显示数量
                if (this._dt.Rows.Count >= this.pageNum * this.pIndex)
                {
                    for (int i = first; i < pageNum * this.pIndex; i++)
                        tmpTable.ImportRow(this._dt.Rows[i]);
                }
                else
                {
                    for (int i = first; i < this._dt.Rows.Count; i++)
                        tmpTable.ImportRow(this._dt.Rows[i]);
                }
                this.grdList.ItemsSource = tmpTable.DefaultView;
                tmpTable.Dispose();
            }
            catch
            {
                MessageBox.Show("错误");
            }
            finally
            {
                DisplayPagingInfo();
            }
        }

        #endregion

        #region 画每页显示等数据

        /// <summary>
        /// 画每页显示等数据
        /// </summary>
        private void DisplayPagingInfo()
        {
            if (this.pIndex == 1)
            {
                this.btnPrev.IsEnabled = false;
                this.btnFirst.IsEnabled = false;

                (this.btnPrev.Parent as Border).IsEnabled = false;
                (this.btnFirst.Parent as Border).IsEnabled = false;

                (this.btnPrev.Parent as Border).Style = this.FindResource("PageBorderUnAble") as Style;
                (this.btnFirst.Parent as Border).Style = this.FindResource("PageBorder") as Style;
            }
            else
            {
                this.btnPrev.IsEnabled = true;
                this.btnFirst.IsEnabled = true;
                (this.btnPrev.Parent as Border).IsEnabled = true;
                (this.btnFirst.Parent as Border).IsEnabled = true;

                (this.btnPrev.Parent as Border).Style = this.FindResource("PageBorder") as Style;
                (this.btnFirst.Parent as Border).Visibility = Visibility.Visible;
                (this.btnFirst.Parent as Border).Style = this.FindResource("PageBorder") as Style;
            }

            if (this.pIndex == this.MaxIndex)
            {
                this.btnNext.IsEnabled = false;
                this.btnLast.IsEnabled = false;
                (this.btnNext.Parent as Border).IsEnabled = false;
                (this.btnLast.Parent as Border).IsEnabled = false;

                (this.btnNext.Parent as Border).Style = this.FindResource("PageBorderUnAble") as Style;
                (this.btnLast.Parent as Border).Style = this.FindResource("PageBorder") as Style;

                if(this.MaxIndex==1)
                    (this.btnLast.Parent as Border).Visibility = Visibility.Collapsed;
            }
            else
            {
                this.btnNext.IsEnabled = true;
                this.btnLast.IsEnabled = true;
                (this.btnNext.Parent as Border).IsEnabled = true;
                (this.btnLast.Parent as Border).IsEnabled = true;

                (this.btnNext.Parent as Border).Style = this.FindResource("PageBorderFour") as Style;
                (this.btnLast.Parent as Border).Visibility = Visibility.Visible;
                (this.btnLast.Parent as Border).Style = this.FindResource("PageBorder") as Style;
            }
            this.tbkRecords.Text = string.Format("每页{0}条/共{1}条", this.pageNum, this.allNum);
            int first = (this.pIndex - 2) > 0 ? (this.pIndex - 2) : 1;
            int last = (first + 4) > this.MaxIndex ? this.MaxIndex : (first + 4);
            this.grid.Children.Clear();

            //添加前面的省略号
            if (first > 1)
            {
                ColumnDefinition cdf = new ColumnDefinition();
                this.grid.ColumnDefinitions.Add(cdf);
                TextBlock tbl = new TextBlock();
                tbl = new TextBlock();
                tbl.Text = "…";
                tbl.Style = FindResource("PageTextBlock3") as Style;

                //绘制外边框
                Border brOut = new Border();
                brOut.Style = FindResource("PageBorder") as Style;
                brOut.Child = tbl;

                Grid.SetColumn(brOut, this.grid.ColumnDefinitions.Count - 1);
                Grid.SetRow(brOut, 0);
                this.grid.Children.Add(brOut);
            }

            for (int i = first; i <= last; i++)
            {
                if (i == 1 || i == MaxIndex)
                {
                    continue;
                }

                ColumnDefinition cdf = new ColumnDefinition();
                this.grid.ColumnDefinitions.Add(cdf);

                TextBlock tbl = new TextBlock();
                tbl.Text = i.ToString();
                tbl.Style = FindResource("PageTextBlock3") as Style;
                tbl.MouseLeftButtonUp += new MouseButtonEventHandler(tbl_MouseLeftButtonUp);
                tbl.MouseLeftButtonDown += new MouseButtonEventHandler(tbl_MouseLeftButtonDown);

                //绘制外边框
                Border brOut = new Border();
                brOut.Style = FindResource("PageBorder") as Style;
                brOut.Child = tbl;

                if (i == this.pIndex)
                {
                    tbl.IsEnabled = false;
                    brOut.IsEnabled = false;
                }

                Grid.SetColumn(brOut, this.grid.ColumnDefinitions.Count - 1);
                Grid.SetRow(brOut, 0);
                this.grid.Children.Add(brOut);
            }
            //添加前面的省略号
            if (last < this.MaxIndex)
            {
                ColumnDefinition cdf = new ColumnDefinition();
                this.grid.ColumnDefinitions.Add(cdf);
                TextBlock tbl = new TextBlock();
                tbl = new TextBlock();
                tbl.Text = "…";
                tbl.Style = FindResource("PageTextBlock3") as Style;

                //绘制外边框
                Border brOut = new Border();
                brOut.Style = FindResource("PageBorder") as Style;
                brOut.Child = tbl;

                Grid.SetColumn(brOut, this.grid.ColumnDefinitions.Count - 1);
                Grid.SetRow(brOut, 0);
                this.grid.Children.Add(brOut);
            }
        }

        #endregion

        #region 首页

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirst_Click(object sender, System.EventArgs e)
        {
            this.pIndex = 1;
            ReadDataTable();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirst_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        #endregion

        #region 上一页
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrev_Click(object sender, System.EventArgs e)
        {
            if (this.pIndex <= 1)
                return;
            this.pIndex--;
            ReadDataTable();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrev_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        #endregion

        #region 下一页

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, System.EventArgs e)
        {
            if (this.pIndex >= this.MaxIndex)
                return;
            this.pIndex++;
            ReadDataTable();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        #endregion

        #region 未页

        /// <summary>
        /// 未页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_Click(object sender, System.EventArgs e)
        {
            this.pIndex = this.MaxIndex;
            ReadDataTable();
        }

        /// <summary>
        /// 未页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        #endregion

        #region 设置最多大页面

        /// <summary>
        /// 设置最多大页面
        /// </summary>
        private void SetMaxIndex()
        {
            //多少页
            int Pages = this._dt.Rows.Count / pageNum;
            if (this._dt.Rows.Count != (Pages * pageNum))
            {
                if (_dt.Rows.Count < (Pages * pageNum))
                    Pages--;
                else
                    Pages++;
            }
            this.MaxIndex = Pages;
            this.btnLast.Text = Pages.ToString();
            this.allNum = this._dt.Rows.Count;
        }

        #endregion

        #region 跳转到多少页

        /// <summary>
        /// 跳转到多少页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnGo_Click(object sender, RoutedEventArgs e)
        //{
        //    if (IsNumber(this.pageGo.Text))
        //    {
        //        int pageNum = int.Parse(this.pageGo.Text);
        //        if (pageNum > 0 && pageNum <= this.MaxIndex)
        //        {
        //            this.pIndex = pageNum;
        //            ReadDataTable();
        //        }
        //        else if (pageNum > this.MaxIndex)
        //        {
        //            this.pIndex = this.MaxIndex;
        //            ReadDataTable();
        //        }
        //    }
        //    this.pageGo.Text = "";
        //}
        #endregion
        #region 改变每页显示的数量值
        private void cbPageNum_Choose(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems.Count > 0)
            {
                var vcb = sender as ComboBox;
                var match = Regex.Match(vcb.SelectedValue.ToString(), @"\d+");
                if (match.Success)
                {
                    pageNum = int.Parse(match.Value.ToString());
                    this.pIndex = 1;
                    SetMaxIndex();
                    ReadDataTable();
                }

            }
        }
        #endregion

        #region 分页数字的点击触发事件

        private void tbl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tbl = sender as TextBlock;
            if (tbl == null)
                return;
            int index = int.Parse(tbl.Text.ToString());
            this.pIndex = index;
            if (index > this.MaxIndex)
                this.pIndex = this.MaxIndex;
            if (index < 1)
                this.pIndex = 1;
            ReadDataTable();
        }

        void tbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        #endregion

        private static Regex RegNumber = new Regex("^[0-9]+$");

        #region 判断是否是数字
        /// <summary>
        /// 判断是否是数字
        /// </summary>
        /// <param name="valString"></param>
        /// <returns></returns>
        public static bool IsNumber(string valString)
        {
            Match m = RegNumber.Match(valString);
            return m.Success;
        }
        #endregion
    }
}
